﻿//定义页面数据类
var pageModel = new Object;



//获取专家分类树形
function getCategorys(str_url) {
    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        success: function (data) {
            //初始化专业专家的所属分类
            $.fn.zTree.init($("#treeCategory"), setting, data);

            //判断当前显示的类型ID是否为空，如果不是，在树形菜单中查找该ID的节点，展开并设置为选中
            var this_id = $("#typeId").val();

            //根据节点的Id，获取节点
            var treeObj = $.fn.zTree.getZTreeObj("treeCategory");
            var nodes = treeObj.getNodesByParam("id", this_id, null);

            //设置当前节点为选定，展开
            if (nodes.length > 0) {
                treeObj.selectNode(nodes[0]);
                treeObj.expandNode(nodes[0], true, true, false);
            } else {
                $("#typeId").val("");
                $("#typePId").val("");
                $("#typeCode").val("");
                $("#typeName").val("");
            }

        }
    });
}

//获取专家分类树形，并在完成后设置界面
function getCategorysAndShowForm(str_url) {
    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        success: function (data) {
            //初始化专业专家的所属分类
            $.fn.zTree.init($("#treeCategory"), setting, data);

            //设置界面
            showForm();
        }
    });
}

function getAExpertTopageModel(str_url) {
    $.ajax({
        type: "GET",
        url: str_url,
        dataType: "json",
        success: function (data) {
            //初始化专业专家的所属分类
            $.fn.zTree.init($("#treeCategory"), setting, data);

            //设置界面
            showForm();
        }
    });
}

function showForm() {
    if (pageModel != null) {
        $("#zjName").val(pageModel.zjName);
        $("input[name='zjSex'][value='" + pageModel.zjSex + "']").attr("checked", "checked");
        $("#zjBirthdayYear").attr("value", pageModel.zjBirthdayYear);
        $("#zjBirthdayMonth").attr("value", pageModel.zjBirthdayMonth);
        $("#zjGraduation").val(pageModel.zjGraduation);
        $("#zjEducation").attr("value", pageModel.zjEducation);
        $("#zjDegree").attr("value", pageModel.zjDegree);
        $("#zjProfessional").val(pageModel.zjProfessional);
        $("#zjCompany").val(pageModel.zjCompany);
        $("#zjPosition").val(pageModel.zjPosition);
        $("#zjTitle").val(pageModel.zjTitle);
        $("#zjResume").val(pageModel.zjResume);
        $("#zjTeleWork").val(pageModel.zjTeleWork);
        $("#zjTeleHome").val(pageModel.zjTeleHome);
        $("#zjTeleFax").val(pageModel.zjTeleFax);
        $("#zjTeleMobile").val(pageModel.zjTeleMobile);
        $("#zjEMail").val(pageModel.zjEMail);
        $("#zjMailAddress").val(pageModel.zjMailAddress);
        $("#zjIDCNumber").val(pageModel.zjIDCNumber);
        $("#zjAreaProvince").attr("value", pageModel.zjAreaProvince);
        $("#zjAreaCity").attr("value", pageModel.zjAreaCity);

        //根据专家分类，设置树形的勾选状态
        var treeObj = $.fn.zTree.getZTreeObj("treeCategory");
        if (treeObj != null) {
            //获取已选分类ID的记录，勾选
            var ids = pageModel.zjCategoryIDs.split(",");
            for (var i = 0, l = ids.length; i < l; i++) {
                var node = treeObj.getNodeByParam("id", ids[i]);
                if (node != null) {
                    treeObj.checkNode(node, true, true);
                }
            }
        }

        $("#zjContentAndProduct").val(pageModel.zjContentAndProduct);
        $("#zjLevel").attr("value", pageModel.zjLevel);
        $("#zjRecommendDepartment").val(pageModel.zjRecommendDepartment);
        $("#zjRecommendDescribe").val(pageModel.zjRecommendDescribe);

    }
}

//根据界面上的数值，获取参数
function getPageModel() {
    pageModel.zjName = $("#zjName").val();
    pageModel.zjSex = $('input:radio[name="zjSex"]:checked').val();
    pageModel.zjBirthdayYear = $("#zjBirthdayYear option:selected").val();
    pageModel.zjBirthdayMonth = $("#zjBirthdayMonth option:selected").val();

    pageModel.zjGraduation = $("#zjGraduation").val();

    pageModel.zjEducation = $("#zjEducation option:selected").val();
    pageModel.zjDegree = $("#zjDegree option:selected").val();


    pageModel.zjProfessional = $("#zjProfessional").val();
    pageModel.zjCompany = $("#zjCompany").val();
    pageModel.zjPosition = $("#zjPosition").val();
    pageModel.zjTitle = $("#zjTitle").val();
    pageModel.zjResume = $("#zjResume").val();
    pageModel.zjTeleWork = $("#zjTeleWork").val();
    pageModel.zjTeleHome = $("#zjTeleHome").val();
    pageModel.zjTeleFax = $("#zjTeleFax").val();
    pageModel.zjTeleMobile = $("#zjTeleMobile").val();
    pageModel.zjEMail = $("#zjEMail").val();
    pageModel.zjMailAddress = $("#zjMailAddress").val();
    pageModel.zjIDCNumber = $("#zjIDCNumber").val();

    pageModel.zjAreaProvince = $("#zjAreaProvince option:selected").val();
    pageModel.zjAreaCity = $("#zjAreaCity option:selected").val();

    //根据专家分类勾选状态，获取专家的分类字符串，以逗号隔开
    var treeObj = $.fn.zTree.getZTreeObj("treeCategory");
    var nodes = treeObj.getCheckedNodes(true);
    var strIDs = "";
    if (nodes != null && nodes.length > 0) {
        for (var i = 0, l = nodes.length; i < l; i++) {
            var halfCheck = nodes[i].getCheckStatus();
            if (!halfCheck.half) {
                if (strIDs == "") {
                    strIDs = nodes[i].id;
                } else {
                    strIDs += "," + nodes[i].id;
                }
            }
        }
    }
    pageModel.zjCategoryIDs = strIDs;

    pageModel.zjContentAndProduct = $("#zjContentAndProduct").val();
    pageModel.zjLevel = $("#zjLevel option:selected").val();
    pageModel.zjRecommendDepartment = $("#zjRecommendDepartment").val();
    pageModel.zjRecommendDescribe = $("#zjRecommendDescribe").val();
}

//初始化专家信息
function initExpert() {
    pageModel = new Object;

    //初始化数据
    pageModel.zjID = "";
    pageModel.zjName = "";
    pageModel.zjSex = "男";
    pageModel.zjBirthdayYear = "1950";
    pageModel.zjBirthdayMonth = "01";
    pageModel.zjGraduation = "";
    pageModel.zjEducation = "大学本科";
    pageModel.zjDegree = "学士";
    pageModel.zjProfessional = "";
    pageModel.zjCompany = "";
    pageModel.zjPosition = "";
    pageModel.zjTitle = "";
    pageModel.zjResume = "";
    pageModel.zjTeleWork = "";
    pageModel.zjTeleHome = "";
    pageModel.zjTeleFax = "";
    pageModel.zjTeleMobile = "";
    pageModel.zjEMail = "";
    pageModel.zjMailAddress = "";
    pageModel.zjIDCNumber = "";
    pageModel.zjAreaProvince = "广西";
    pageModel.zjAreaCity = "南宁";
    //所属专家分类ID列表
    pageModel.zjCategoryIDs = "";
    pageModel.zjContentAndProduct = "";
    pageModel.zjLevel = "地方级";
    pageModel.zjRecommendDepartment = "";
    pageModel.zjRecommendDescribe = "";
    pageModel.zjStopStatus = "";
    pageModel.zjStopDescribe = "";
}

function saveExpertAsNew() {
    if (pageModel != null) {
        if (pageModel.zjName == "") {
            alertMSG("专家姓名必须填写！");
        } else {
            $.ajax({
                type: "POST",
                url: strUrlnewAExpert,
                dataType: "json",
                data: JSON.stringify(pageModel),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.ISOK) {
                        pageModel.zjName = "";
                        showForm();
                        alertMSG("保存专家信息成功！");
                    } else {
                        alertMSG("保存专家记录失败，错误代码<br>" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alertMSG(textStatus);
                }
            });
        }
    }
}


function saveExpertAsEdit() {
    if (pageModel != null) {
        if (pageModel.zjName == "") {
            alertMSG("专家姓名必须填写！");
        } else {
            $.ajax({
                type: "POST",
                url: strUrleditAExpert,
                dataType: "json",
                data: JSON.stringify(pageModel),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    if (data.ISOK) {
                        pageModel.zjName = "";
                        showForm();
                        alertMSG("保存专家信息成功！");
                    } else {
                        alertMSG("保存专家记录失败，错误代码<br>" + data.MESSAGE);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alertMSG(textStatus);
                }
            });
        }
    }
}

//清除数据列表
function clearData() {
    $("#tableDatas").html("");
}

//获取查询条件，查询专家列表
function queryExperts() {
    objQuery.qName = $("#qName").val();
    objQuery.qProfessional = $("#qProfessional").val();
    objQuery.qCompany = $("#qCompany").val();
    objQuery.qStatus = $("#qStatus option:selected").val();

    //获取专家所属分类ID
    objQuery.qCategoryIDs = "";

    var treeObj = $.fn.zTree.getZTreeObj("treeCategory");
    if (treeObj == null) {
        alertMSG("分类未获取！请再试一次");
        return;
    }

    var nodes = treeObj.getCheckedNodes();
    for (i = 0; i < nodes.length; i++) {
        if (objQuery.qCategoryIDs == "") {
            objQuery.qCategoryIDs = nodes[i].id;
        } else {
            objQuery.qCategoryIDs += "," + nodes[i].id;
        }
    }

    //查询
    $.ajax({
        type: "POST",
        url: strUrlgetExperts,
        dataType: "json",
        data: JSON.stringify(objQuery),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.ISOK) {
                clearData();
                //将数据填充表格模板
                $('#DataTemplate').tmpl(data.experts).appendTo('#tableDatas');

                objQuery.recordCount = data.recordCount;
                objQuery.pageCount = Math.ceil(objQuery.recordCount / objQuery.pageSize);
                if (objQuery.pageIndex > objQuery.pageCount && objQuery.pageCount > 0) {
                    objQuery.pageIndex = objQuery.pageCount - 1;
                } else if (objQuery.pageCount == 0) {
                    objQuery.pageIndex = 0;
                }

                //查询完毕后，设置分页
                $("#pageGO").val(objQuery.pageIndex + 1);
                $("#pageCount").html(objQuery.pageCount);
            } else {
                clearData();
                alertMSG("查询失败，错误代码<br>" + data.MESSAGE);
                objQuery.recordCount = 0;
                objQuery.pageCount = 0;

                //查询完毕后，设置分页
                $("#pageGO").val(objQuery.pageIndex + 1);
                $("#pageCount").html(objQuery.pageCount);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alertMSG(textStatus + "<br />错误信息：" + errorThrown);
        }
    });
}

//执行查询
function doQuery() {
    //查询
    queryExperts();

}

//上一页
function go_p() {
    if (objQuery.pageIndex > 0) {
        objQuery.pageIndex--;
        //查询
        queryExperts();
        //查询完毕后，设置分页
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    } else {
        alertMSG("已经是第一页了。");
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    }
}
//下一页
function go_n() {
    if (objQuery.pageIndex < objQuery.pageCount - 1) {
        objQuery.pageIndex++;
        //查询
        queryExperts();
        //查询完毕后，设置分页
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    } else {
        alertMSG("已经是最后一页了。");
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    }
}
//转到指定页面
function go() {
    objQuery.pageIndex = $("#pageGO").val() - 1;
    if (objQuery.pageIndex < objQuery.pageCount && objQuery.pageIndex >= 0) {
        objQuery.pageIndex = $("#pageGO").val() - 1;

        //查询
        queryExperts();
        //查询完毕后，设置分页
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    } else {
        alertMSG("页码不正确。");
        $("#pageGO").val(objQuery.pageIndex + 1);
        $("#pageCount").html(objQuery.pageCount);
    }
}


//检查专家姓名是否重复
function checkExpertName(expertName) {
    $.ajax({
        type: "GET",
        url: strUrlcheckAExpert,
        dataType: "json",
        data: { expertName: expertName },
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.ISOK) {
                alertMSG(data.MESSAGE);
            } else {
                alertMSG("保存专家记录失败，错误代码<br>" + data.MESSAGE);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alertMSG(textStatus);
        }
    });
}